//
//  HJTabBar.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/5.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit

class HJTabBar: UITabBar {

    
    //定义闭包变量
    var composeButtonClosure: (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    func setupUI(){
        
        backgroundImage = UIImage(named: "tabbar_background")
        addSubview(composeButton)
    }
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        composeButton.center = CGPoint(x: frame.width * 0.5, y: frame.height * 0.5)
        
        
        let childW = frame.width/5
        var index = 0
        for childView in subviews{
            
            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                let x = childW * CGFloat(index)
                childView.frame = CGRect(x: x, y: 0, width: childW, height: frame.height)
                
                index++
                
                if index == 2{
                    index++
                }
            }
        }
        
    }
    
    
    lazy var composeButton: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: "composeButtonClick", forControlEvents: .TouchUpInside)
        
        //设置button的背景颜色
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        //设置button的tup
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        button.sizeToFit()
        
        return button
    }()
    
    // MARK: -监听点击
    @objc private func composeButtonClick(){
        
        composeButtonClosure?()
    }
}
